/**
 * promise的基本使用
 */

// const promise = new Promise((resolve, reject) => {
//     // ... some code
//   if (/* 异步操作成功 */) {
//       resolve(value)
//   }else{
//     reject(err)
//   }
// })


function timeout(ms) {
  return new Promise((resolve, reject) => {
    console.log(reject)

    // 不能这样判断, 这样判断会走else分支, 为什么它会走到else分支呢?
    // if (reject === null || reject === undefined) {
    //   return setTimeout(resolve, ms, 'done')
    // } else {
    //   return setTimeout(reject, ms, 'error done')
    // }

    setTimeout(resolve, ms, 'done')
  })
}

timeout(100).then((value) => {
  console.log(value)
})

const p1 = new Promise((resolve, reject) => {
  // resolve(1)
  reject(1)
})


p1.then(value => {
  console.log('onResolved01()', value)
  return 2
}).catch(reason => {
  console.log('error', reason)
  return new Promise(() => {
  })
}).then(value => {
  console.log('onResolved02()', value)
  return 3
}).then(value => {
  console.log('onResolved03()', value)
  return 4
}).catch(reason => {
  console.log('onRejectedAll()', reason)
})

// then()方法不写onRejected失败的回调函数, 有一个默认的 reason => {throw reason} 失败回调函数
// p1.then(value => {
//   console.log('onResolved01()', value)
//   return 2
// }, reason => {
//   throw reason
// }).then(value => {
//   console.log('onResolved02()', value)
//   return 3
// }, reason => {
//   throw reason
// }).then(value => {
//   console.log('onResolved03()', value)
//   return 4
// }, reason => {
//   throw reason
// }).catch(reason => {
//   console.log('onRejectedAll()', reason)
// })
